package com.yh.base.util;

import java.security.MessageDigest;

import org.apache.catalina.util.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 编码功能
 * @author hujun
 *
 */
public class EncoderUtil {
    private final static Log log = LogFactory.getLog(EncoderUtil.class);

    private static MessageDigest messageDigest = null;

    /**
     * 对输入参数经行MD5编码
     * @param str
     * @return
     */
    public static String getMd5Encoder(String str) {
        String retValue = "";
        try {
            if (messageDigest == null) {
                messageDigest = MessageDigest.getInstance("MD5");
            }
            messageDigest.update(str.getBytes("UTF-8"));
            byte[] digest = messageDigest.digest();
            byte[] encoded = Base64.encode(digest);
            retValue = new String(encoded);
        } catch (Exception e) {
            log.error("MD5编码出现错误");
            log.error(e);
        }
        return retValue;
    }
}
